import menuFunction from '@/js/MenuFunction'
import ConstDef from '@/js/ConstDef'
import { ElMessage } from 'element-plus/dist/index.full'
import nvFilterService from '@/api/NvFilterService'
import cacheManage from '@/cache/CacheManage'

class FilterUtils {
  getSourceFdNames(expression) {
    const fdNames = [];
    const filterFds = expression.filterFds;
    for (const filterFd of filterFds) {
      if (filterFd.value && filterFd.value.toString().startsWith('@')) {
        fdNames.push(filterFd.value.substr(1, filterFd.value.length - 1))
      }
    }
    return fdNames;
  }

  async filter(expression, sourceNv) {
    function getFilterFdInConfig(fdName, config) {
      for (const conditionFd of config.filterConditionFdNd) {
        if (conditionFd.fdAlias === fdName) { return conditionFd; }
      }
      for (const conditionFd of config.filterConditionFdNd) {
        if (conditionFd.fdName === fdName) { return conditionFd; }
      }
      return null;
    }
    function explainFilterFd(filterFd) {
      if (filterFd.value && filterFd.value.toString().startsWith('@')) {
        const sourceFdName = filterFd.value.substr(1, filterFd.value.length - 1);
        if (sourceFdName in sourceNv) filterFd.value = sourceNv[sourceFdName]; else filterFd.value = null;
      }
    }
    function getFilterFds(config) {
      const filterFds = expression.filterFds;
      const realFilterFds = [];
      for (const filterFd of filterFds) {
        const conditionFd = getFilterFdInConfig(filterFd.fdName, config);
        if (conditionFd == null) {
          ElMessage({
            message: `查询下拉康配置查询条件属性：${filterFd.fdName}不在查询定义的查询定义:${config.filterDefCode}中！`,
            type: 'error'
          })
          continue;
        }
        const realFilterFd = { ...filterFd };
        realFilterFd.logicalOperationFlag = conditionFd.logicalOperationFlag;
        explainFilterFd(realFilterFd);
        realFilterFds.push(realFilterFd);
      }
      return realFilterFds;
    }
    const filterDefCode = expression.filterDefCode;
    const configMsg = await menuFunction.getFilterConfig(ConstDef.FILTER_FUNCTION, filterDefCode);
    if (!configMsg.success) return;
    const config = configMsg.value;
    const filterFds = getFilterFds(config);
    const commonInfo = cacheManage.getDefaultCommonInfo();
    const filterVO = {
      filterDefCode,
      filterFds,
      resultFds: [],
      startNo: 0,
      count: 999
    }
    const nvsMsg = await nvFilterService.filterNvsByDef(commonInfo, filterVO);
    if (!nvsMsg.success) return nvsMsg;
    return nvsMsg;
  }

  async filterOptions(expression, sourceNv) {
    const nvsMsg = await this.filter(expression, sourceNv);
    if (!nvsMsg.success) return nvsMsg;
    if (!expression.transfer) { return { success: true, value: nvsMsg.value.nvs }; }
    const options = [];
    for (const nv of nvsMsg.value.nvs) {
      options.push({
        code: nv[expression.codeFdName],
        description: nv[expression.descriptionFdName]
      })
    }
    return {
      success: true,
      value: options
    }
  }
}
const filterUtils = new FilterUtils();
export default filterUtils;
